import axios from 'axios';
import { useTokenStore } from '@/stores/token'; // 根据你的路径调整

const API_BASE_URL = '/api/patriarch'; // 使用代理路径

const getAuthHeaders = () => {
    const tokenStore = useTokenStore();
    return {
        Authorization: ` ${tokenStore.token}`
    };
};

// 新增子女关联
export const addRelevance = async (studentId) => {
    try {
        const response = await axios.post(`${API_BASE_URL}/addRelevance`, { studentId }, {
            headers: getAuthHeaders()
        });
        return response.data;
    } catch (error) {
        console.error('Error adding relevance:', error.response?.data || error.message);
        throw new Error('Error adding relevance');
    }
};

// 获取子女关联列表
export const fetchRelevanceList = async () => {
    try {
        const response = await axios.get(API_BASE_URL, {
            headers: getAuthHeaders()
        });
        return response.data;
    } catch (error) {
        console.error('Error fetching relevance list:', error.response?.data || error.message);
        throw new Error('Error fetching relevance list');
    }
};

// 解除子女关联
export const deleteRelevanceById = async (id) => {
    try {
        const response = await axios.delete(API_BASE_URL, {
            params: { id },
            headers: getAuthHeaders()
        });
        return response.data;
    } catch (error) {
        console.error('Error deleting relevance:', error.response?.data || error.message);
        throw new Error('Error deleting relevance');
    }
};
